package de.game.activities;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import de.game.GameEngine;
import de.game.PaintThread;
import de.game.R;
import de.game.Util;
import de.game.gridlogic.ASearch;
import de.game.util.Util_Date;

public class GameActivity extends Activity {

	private de.game.GameView v_game;
	
	private ImageButton btn_down, btn_up, btn_right, btn_left, btn_start_pause;
	private TextView tv_time;
	public static int gameTimeSecs = 0;
	
	private boolean pause = true;
	
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// Activity Livecycle
	// ------------------------------------------------------------------------------------------------------------------------------------------		
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.game);
						
		v_game = (de.game.GameView) findViewById(R.id.v_game);	
		v_game.setGameActivity(this);
		v_game.initView();
		
		tv_time = (TextView) findViewById(R.id.tv_time);
		
		activateClickListener();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	// ------------------------------------------------------------------------------------------------------------------------------------------
	// 
	// ------------------------------------------------------------------------------------------------------------------------------------------	
	
	public void activateClickListener(){
		btn_down = (ImageButton) findViewById(R.id.btn_down);
		btn_down.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// hole die alte spielerposition
				int newXkord = ASearch.getTarget()[0];	
				int newYkord = ASearch.getTarget()[1];
				
				if(!(newYkord+1 > v_game.getgEngine().getGridLogicZeilen()-1) && 
						!v_game.getgEngine().getGridGraphic()[newXkord][newYkord+1].isWand()){
					ASearch.setTarget(new int[] {newXkord, newYkord+1});
				}
				return;
			}
			
		});
		
		btn_up = (ImageButton) findViewById(R.id.btn_up);
		btn_up.setOnClickListener(new OnClickListener() {		
			@Override
			public void onClick(View v) {
				// hole die alte spielerposition
				int newXkord = ASearch.getTarget()[0];	
				int newYkord = ASearch.getTarget()[1];
				
				if(!(newYkord-1 < 0) && !v_game.getgEngine().getGridGraphic()[newXkord][newYkord-1].isWand()){
					ASearch.setTarget(new int[] {newXkord, newYkord-1});	
				}
				return;
			}
		});
		
		btn_right = (ImageButton) findViewById(R.id.btn_right);
		btn_right.setOnClickListener(new OnClickListener() {		
			@Override
			public void onClick(View v) {
				// hole die alte spielerposition
				int newXkord = ASearch.getTarget()[0];	
				int newYkord = ASearch.getTarget()[1];
				
				if(!(newXkord+1 > v_game.getgEngine().getGridLogicSpalten()-1) && 
						!v_game.getgEngine().getGridGraphic()[newXkord+1][newYkord].isWand()){
					ASearch.setTarget(new int[] {newXkord+1, newYkord});
				}
				return;
			}
		});
		
		btn_left = (ImageButton) findViewById(R.id.btn_left);
		btn_left.setOnClickListener(new OnClickListener() {		
			@Override
			public void onClick(View v) {
				// hole die alte spielerposition
				int newXkord = ASearch.getTarget()[0];	
				int newYkord = ASearch.getTarget()[1];
				
				if(!(newXkord-1 < 0) && !v_game.getgEngine().getGridGraphic()[newXkord-1][newYkord].isWand()){				
					ASearch.setTarget(new int[] {newXkord-1, newYkord});
				}
				return;
			}
		});
		
		btn_start_pause = (ImageButton) findViewById(R.id.btn_start_pause);
		Util.startViewFadeAnimation(GameActivity.this, btn_start_pause);
		btn_start_pause.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				pause = !pause;
				
				if(pause){
					
					btn_start_pause.setImageResource(R.drawable.icon_start);
					Util.startViewFadeAnimation(GameActivity.this, btn_start_pause);
					
					runOnUiThread(new Runnable() {						
						@Override
						public void run() {
							v_game.stopThread();							
						}
					});
					
				} else {
					
					btn_start_pause.setImageResource(R.drawable.icon_pause);
					Util.stopViewFadeAnimation(btn_start_pause);
					
					runOnUiThread(new Runnable() {						
						@Override
						public void run() {							
							v_game.startOrResumeThread();							
						}
					});
					
				}
			}
		});
		
	}

	public TextView getTv_time() {
		return tv_time;
	}
	
	public void showGameTime(){		
		runOnUiThread(new Runnable() {			
			@Override
			public void run() {
				long gameTime = GameEngine.currentTime - GameEngine.startTime; 
				long diffSeconds = gameTime / 1000;
				long diffMinutes = gameTime / (60 * 1000);
				
				if(diffSeconds >= 1){
					tv_time.setText(diffMinutes+":"+diffSeconds);
				}
				
			}
		});
	}

	public void setPause(boolean b) {
		this.pause = b;		
	}

	public de.game.GameView getV_game() {
		return v_game;
	}

	
}
